/* eslint-disable consistent-return */
<template>
  <div class="show-table">
    <el-table
      ref="tableRef"
      :data="auditData"
      height="300px"
      row-key="key"
      size="mini"
      border
      :header-cell-style="{background:'#E2EBF0',color: '#000'}"
      style="width: 100%"
      :tree-props="{children: 'point'}"
      @select="rowSelect"
      @select-all="selectAll"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="type"
        align="center"
        label="资源类型"
        width="120px"
      />
      <el-table-column
        prop="name"
        align="center"
        label="资源名称"
        min-width="140px"
      />
      <!-- <el-table-column
        prop="check_account"
        label="审核人"
        align="center"
        min-width="80px"
      />
      <el-table-column
        prop="seg_name"
        label="审核环节"
        align="center"
        min-width="80px"
      />
      <el-table-column
        prop="check_date"
        label="审核时间"
        align="center"
        min-width="80px"
      />
      <el-table-column
        prop="is_check"
        align="center"
        label="是否通过"
        min-width="80px"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.is_check === undefined"
            class="no-audit"
          >
            未审核
          </span>
          <span
            v-else-if="scope.row.is_check === '1'"
            class="pass"
          >
            通过
          </span>
          <span
            v-else-if="scope.row.is_check === '0'"
            class="no-pass"
          >
            未通过
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="check_reason"
        align="center"
        label="审核意见"
        min-width="180px"
      /> -->
    </el-table>
  </div>
</template>

<script>
import { allAuditResult } from '../../../../../../../../api/service/gisShow2020';

export default {
  name: 'ShowAudit',
  props: {
    auditData: {
      type: Array,
      default: () => [],
    },
    planId: {
      type: String,
      default: '',
    },
    localConfig: {
      type: Object,
      default: () => {},
    },
    activeId: {
      type: String,
      default: '',
    },
    activeName: {
      type: String,
      default: '',
    },
    useraccount: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      // 审批资源列表
      auditList: [],
    };
  },
  mounted() {
    this.showAuditData();
  },
  methods: {
    showAuditData() {
      console.log(this.auditData);
    },
    // 表格多选
    toggleSelection(rows, flag) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.tableRef.toggleRowSelection(row, flag);
        });
      } else {
        this.$refs.tableRef.clearSelection();
      }
    },
    getAllSelect(isCheck) {
      this.auditList = this.$refs.tableRef.store.states.selection;
      if (this.auditList.length > 0) {
        const a = this.arrayNonRepeatfy(this.auditList);
        const params = [];
        a.forEach((res) => {
          params.push({
            seg_id: this.activeId,
            seg_name: this.activeName,
            plan_id: this.planId,
            check_account: this.useraccount,
            modify_account: this.useraccount,
            resource_id: res.id,
            resource_type: res.typeEn,
            resource_name: res.name,
            is_check: Number(isCheck),
            check_reason: Number(isCheck) === 1 ? '通过' : '不通过',
          });
        });
        const param = {
          data: params,
          check: isCheck,
        };
        console.log(param);
        allAuditResult(param);
      } else {
        this.$message.info('未选择审核资源');
      }
      this.$emit('closed');
    },
    arrayNonRepeatfy(arr) {
      const map = new Map();
      const array = []; // 数组用于返回结果
      for (let i = 0; i < arr.length; i += 1) {
        if (map.has(arr[i].id)) { // 如果有该key值
          map.set(arr[i].id, true);
        } else {
          map.set(arr[i].id, false); // 如果没有该key值
          array.push(arr[i]);
        }
      }
      return array;
    },
    rowSelect(selection, row) {
      if (selection.indexOf(row) > -1 && row.point && row.point.length > 0) {
        this.toggleSelection(row.point, true);
      }
      if (selection.indexOf(row) === -1 && row.point && row.point.length > 0) {
        this.toggleSelection(row.point, false);
      }
      if (selection.indexOf(row) > -1 && row.point === undefined) {
        const s = [];
        this.auditData.forEach((lineData) => {
          if (lineData.point && lineData.point.length > 0) {
            lineData.point.forEach((poi) => {
              if (poi.id === row.id && row.key === lineData.id + poi.id) {
                s.push(lineData);
              }
            });
          }
        });
        this.toggleSelection(s, true);
      }
    },
    selectAll(selection) {
      let flag = false; // 默认 为全不选
      selection.forEach((item) => {
        if (item.point && item.point.length > 0) {
          flag = true;
          this.toggleSelection(item.point, true);
        }
      });
      if (!flag) {
        this.toggleSelection();
      }
    },
  },
};
</script>

<style>

</style>
